<template>
  <div class="app-container">
    <el-tabs v-model="queryParams.pooled" @tab-click="handleTabClick">
      <el-tab-pane label="线索" name="clueTab"></el-tab-pane>
      <el-tab-pane label="线索池" name="pooled"></el-tab-pane>
    </el-tabs>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客户姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入客户姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="渠道" prop="channel">
        <el-select placeholder="请输入渠道来源" clearable v-model="form.channel">
          <el-option v-for="item in dict.type.clues_item" :label="item.label" :value="item.value" :key="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="意向学科" prop="subject">
        <el-select placeholder="请输入意向学科" clearable v-model="form.subject">
          <el-option v-for="item in dict.type.course_subject" :label="item.label" :value="item.value" :key="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" v-if="queryParams.pooled=='clue'" prop="subject">
        <el-select placeholder="请输入状态" clearable v-model="queryParams.status">
          <el-option label="待分配" value="0"></el-option>
          <el-option label="已分配" value="1"></el-option>
          <el-option label="进行中" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" v-if="queryParams.pooled=='pooled'" prop="subject">
        <el-select placeholder="请输入状态" clearable v-model="queryParams.status">
          <el-option label="已回收" value="3"></el-option>
          <el-option label="伪线索" value="4"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['crm:clue:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['crm:clue:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['crm:clue:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['crm:clue:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleBatchAssign"
          v-hasPermi="['crm:clue:edit']"
        >批量分配线索</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleBatchAdd"
          v-hasPermi="['crm:clue:add']"
        >批量新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="clueList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="线索id" align="center" prop="id" />
      <el-table-column label="客户姓名" align="center" prop="name" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="渠道" align="center" prop="channel" :formatter="channelFormatter" />
      <el-table-column label="意向学科" align="center" :formatter="subjectFormatter" />
      <el-table-column label="状态(已分配1  进行中2  回收3  伪线索4)" align="center" :formatter="statusFormatter"  />
      <el-table-column label="归属" align="center" prop="owner"  />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleView(scope.row)"
            v-hasPermi="['crm:clue:query']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleTrack(scope.row)"
            v-hasPermi="['crm:clue:track']"
            v-if="scope.row.status=='1' || scope.row.status=='2'"
          >跟进</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleAssign([scope.row.id])"
            v-hasPermi="['crm:clue:edit']"
            v-if="scope.row.status=='1' || scope.row.status=='0'"
          >分配</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleReset(scope.row)"
            v-hasPermi="['crm:clue:edit']"
            v-if="(scope.row.status=='3' || scope.row.status=='4') && scope.row.falseCount<3"
          >捞取</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改线索对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="客户姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入客户姓名" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="渠道" prop="channel">
          <el-input v-model="form.channel" placeholder="请输入渠道" />
        </el-form-item>
        <el-form-item label="活动id" prop="activityId">
          <el-input v-model="form.activityId" placeholder="请输入活动id" />
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入年龄" />
        </el-form-item>
        <el-form-item label="微信" prop="weixin">
          <el-input v-model="form.weixin" placeholder="请输入微信" />
        </el-form-item>
        <el-form-item label="qq" prop="qq">
          <el-input v-model="form.qq" placeholder="请输入qq" />
        </el-form-item>
        <el-form-item label="意向等级" prop="level">
          <el-input v-model="form.level" placeholder="请输入意向等级" />
        </el-form-item>
        <el-form-item label="意向学科" prop="subject">
          <el-input v-model="form.subject" placeholder="请输入意向学科" />
        </el-form-item>
        <el-form-item label="伪线索失败次数(最大数3次)" prop="falseCount">
          <el-input v-model="form.falseCount" placeholder="请输入伪线索失败次数(最大数3次)" />
        </el-form-item>
        <el-form-item label="${comment}" prop="nextTime">
          <el-date-picker clearable
            v-model="form.nextTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择${comment}">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否转派" prop="transfer">
          <el-input v-model="form.transfer" placeholder="请输入是否转派" />
        </el-form-item>
        <el-form-item label="线索失效时间" prop="endTime">
          <el-date-picker clearable
            v-model="form.endTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择线索失效时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-drawer
    :visible.sync="drawer"
    :with-header="false"
    size="70%">
<!--  :clueId="clueId" 从父组件里面传递到子组件去    -->
      <track-drawer ref="trackDrawer" :view="view" :clueId="clueId" @success="getList" @cancel="drawer=false"></track-drawer>
    </el-drawer>
    <assign-dialog ref="assignDialog"
                   @ok="getList"
                   :type="assignType"
                   :title="assignTitle"
    ></assign-dialog> <!-- 使用 -->
    <batch-add ref="batchAddDialog" @success="getList"></batch-add>
  </div>
</template>

<script>
import { listClue, getClue, delClue, addClue, updateClue } from "@/api/crm/clue";
import AssignDialog from '@/views/crm/clue/components/assign' // 申明
import TrackDrawer from '@/views/crm/clue/components/track'
import BatchAdd from '@/views/crm/clue/components/batch-add'
import {resetClue} from '@/api/crm/clueTrack'
export default {
  name: "Clue",
  dicts:['course_subject','clues_item','clue_status'],
  components:{AssignDialog,TrackDrawer,BatchAdd},// 注册组件
  data() {
    return {
      assignType:"CLUE",
      assignTitle:'线索',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 线索表格数据
      clueList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        phone: null,
        channel: null,
        activityId: null,
        sex: null,
        age: null,
        weixin: null,
        qq: null,
        level: null,
        subject: null,
        status: null,
        falseCount: null,
        nextTime: null,
        transfer: null,
        endTime: null,
        pooled:'clueTab',
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      clueId:null,
      drawer:false,
      view:true,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询线索列表 */
    getList() {
      this.loading = true;
      listClue(this.queryParams).then(response => {
        this.clueList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        phone: null,
        channel: null,
        activityId: null,
        sex: null,
        age: null,
        weixin: null,
        qq: null,
        level: null,
        subject: null,
        status: null,
        createTime: null,
        createBy: null,
        falseCount: null,
        nextTime: null,
        updateTime: null,
        transfer: null,
        endTime: null,
        pooled:'clueTab',
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加线索";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getClue(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改线索";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateClue(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addClue(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除线索编号为"' + ids + '"的数据项？').then(function() {
        return delClue(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('crm/clue/export', {
        ...this.queryParams
      }, `clue_${new Date().getTime()}.xlsx`)
    },
    subjectFormatter(row){
      return this.selectDictLabel(this.dict.type.course_subject,row.subject);
    },
    channelFormatter(row){
      return this.selectDictLabel(this.dict.type.clues_item,row.channel);
    },
    statusFormatter(row){
      return this.selectDictLabel(this.dict.type.clue_status,row.status);
    },
    // 分配线索
    handleAssign(row){
      this.$refs.assignDialog.open=true
      this.$refs.assignDialog.resetForm()
      this.$refs.assignDialog.ids=row;
    },
    // 批量分配线索
    handleBatchAssign(){
      if (this.ids && this.ids.length>0){
        this.handleAssign(this.ids)
      }else {
        this.$modal.alert("请至少选择一项")
      }
    },
    handleBatchAdd(){
      this.$refs.batchAddDialog.open=true
      this.$refs.batchAddDialog.resetForm()
    },
    handleTrack(row){
      this.drawer=true
      this.clueId=row.id
      this.view=false
    },
    //查看
    handleView(row){
      this.view=true
      this.drawer=true
      this.clueId=row.id
    },
    handleTabClick(){
      this.getList()
    },
    // 捞取
    handleReset(row){
      this.$modal.confirm(`确定捞取线索${row.id}吗？`).then(()=>{
        resetClue(row.id).then(req=>{
          this.$message.success("捞取线索成功")
          this.getList()
        })
      })
    }
  }
};
</script>
